// packageH/chitchat/components/ChatContentRecord/ChatContentRecord.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item:{
      type:Object,
      value:{}
    },
    emojiObjs:{
      type:Object,
      value:{}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
     show_list:[]
  },

 ready(){
   let list = this.data.item.content.content_list
   list = this.fmtChatList(list)
  this.setData({
    show_list:list
  })
 },

  /**
   * 组件的方法列表
   */
  methods: {
    toChatRecord(e){
      let history_queue_id = e.currentTarget.dataset.history_queue_id
      wx.navigateTo({
        url: '/packageH/chitchat/chatRecordDetail/chatRecordDetail?history_queue_id=' + history_queue_id + '&emojiObjs=' + encodeURIComponent(JSON.stringify(this.data.emojiObjs)) 
      });
    },
    fmtChatList(chatList) {
      // 处理商品或表情消息
      if (chatList instanceof Array) {
        for (let i = 0; i < chatList.length; i++) {
            if (chatList[i]["content_type"] == 0) {
            chatList[i]["content"] = this.replaceEmoji(chatList[i]["content"]);
          }
        }
      } else if (chatList instanceof Object) {
           if (chatList["content_type"] == 0) {
          chatList["content"] = this.replaceEmoji(chatList["content"]);
        }
      }
      return chatList;
    },
    replaceEmoji(content) {
      let that = this
      return content.replace(/face\[([^\s\[\]]+?)\]/g, function (i) {
        return '<img src="' + that.data.emojiObjs[i]["url"] + '" style="width:30px;height:30px;"/>';
      });
    }
  }
})
